﻿using System;
using System.Linq;
using System.Collections.Generic;
using VirtualTournaments.Business.Tournaments.Exceptions;

namespace VirtualTournaments.Business.Tournaments
{
	public abstract class Tournament : ICompetition
	{
		private string _name;
		private bool _isFinished;
		private bool _isActive;
		private Team _first;
		private Team _second;
		private Team _third;
		private List<Team> _teams;
		private Fixture _fixture;
		private MatchFactory _factory;
        private int _lastJourneyNumber;

		public Tournament(String name, IEnumerable<Team> teams, MatchFactory factory)
		{
			Teams = teams;
			Factory = factory;
			IsFinished = false;
            LastJourneyNumber = 0;
            Name = name;
		}

        public Journey GetLastJourney()
        {
            if (LastJourneyNumber == 0)
                throw new NotStartedTournamentException();
            return Fixture.GetJourney(LastJourneyNumber);
        }

        public Journey GetNextJourney()
        {
            if (LastJourneyNumber == Fixture.Count())
                throw new FinishedTournamentException();
            return Fixture.GetJourney(LastJourneyNumber + 1);
        }

		public abstract void PlayNextJourney();

		protected MatchFactory Factory
		{
			get { return _factory; }
			set { _factory = value; }
		}

		public string Name
		{
			get { return _name; }
			set { _name = value; }
		}

		public bool IsFinished
		{
			get { return _isFinished; }
			protected set { _isFinished = value; }
		}

		public bool IsActive
		{
			get { return _isActive; }
			protected set { _isActive = value; }
		}

		public virtual Team First
		{
			get { return _first; }
			protected set { _first = value; }
		}

		public virtual Team Second
		{
			get { return _second; }
			protected set { _second = value; }
		}

		public virtual Team Third
		{
			get { return _third; }
			protected set { _third = value; }
		}

		public virtual Fixture Fixture
		{
			get { return _fixture; }
			protected set { _fixture = value; }
		}

		public virtual IEnumerable<Team> Teams
		{
			get { return _teams; }
			protected set { _teams = value.ToList(); }
		}

        public int LastJourneyNumber
		{
			get { return _lastJourneyNumber; }
			protected set { _lastJourneyNumber = value; }
		}

		protected List<Team> TeamsList
		{
			get { return _teams; }
			set  { _teams = value; }
		}

		public bool HasFinished()
		{
			return IsFinished;
		}
	}
}
